package com.esd.bs.controller;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esd.util.Util;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

/**
 * 公众来信
 * @author liukai
 *
 */
@Controller
@RequestMapping(value = "/web")
public class AdviceController {

	private static Logger logger = Logger.getLogger(AdviceController.class);

	private WebClient webClient = null;
	
	private final String URL = "http://www.baic.gov.cn/mess/message/leavemess_new.jsp";

	public AdviceController() {
		webClient = new WebClient();
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.getOptions().setCssEnabled(true);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.getOptions().setThrowExceptionOnScriptError(false);
	}
	
	/**
	 * 建议 htmlunit模拟提交,重点**验证码的获取
	 * @param url
	 * @param author
	 * @param email
	 * @param mobile
	 * @param address
	 * @param type
	 * @param show
	 * @param title
	 * @param content
	 * @param certicode
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/submitPage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> submitPagePOST(String author, String email, String mobile, String address, String type, String show, String title, String content, String certicode, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String randomCode = request.getSession().getAttribute("randomCode").toString();
		if (!randomCode.equals(certicode)) {
			map.put("notice", false);
			map.put("message", "验证码错误!");
			return map;
		}
		if (content == null || content.equals("")) {
			map.put("notice", false);
			map.put("message", "请输入内容!");
			return map;
		}
		try {
			HtmlPage page = webClient.getPage(URL);
			// 加载js时间
			HtmlInput author_input = (HtmlInput) page.getElementById("author");
			author_input.setValueAttribute(author);

			HtmlInput email_input = (HtmlInput) page.getElementById("email");
			email_input.setValueAttribute(email);

			HtmlInput mobile_input = (HtmlInput) page.getElementById("mobile");
			mobile_input.setValueAttribute(mobile);

			HtmlInput address_input = (HtmlInput) page.getElementById("address");
			address_input.setValueAttribute(address);

			HtmlRadioButtonInput type_input = (HtmlRadioButtonInput) page.getElementByName("type");
			type_input.setValueAttribute(type);

			HtmlInput show_input = (HtmlInput) page.getElementByName("show");
			show_input.setValueAttribute(show);

			HtmlInput title_input = (HtmlInput) page.getElementByName("title");
			title_input.setValueAttribute(title);

			HtmlTextArea content_textare = (HtmlTextArea) page.getElementByName("content");
			content_textare.setTextContent(content);
			// 验证码
			HtmlTextInput yzmInput = (HtmlTextInput) page.getElementById("certicode");
			HtmlImage htmlImage = (HtmlImage) page.getElementById("verifyImage");
			// 下载验证码图片
			String yzmNum = Util.getCodeNum(htmlImage);
			// 填写验证码
			yzmInput.setValueAttribute(yzmNum);
			// 提交按钮
			HtmlSubmitInput submit = page.getElementByName("Submit");
			HtmlPage nextPage = submit.click();	
			// 转换
			if (nextPage != null) {
				String pageUrl = nextPage.getUrl().toString().trim();
				if (pageUrl.equals("http://www.baic.gov.cn/mess/message/leavemess_new.jsp")) {
					map.put("notice", true);
					map.put("message", "提交成功!");
				}
			} else {
				map.put("notice", false);
				map.put("message", "程序出现错误!请稍后重试");
			}
		} catch (FailingHttpStatusCodeException e) {
			logger.error(e.getMessage());
		} catch (MalformedURLException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			if (webClient != null) {
				webClient.close();
			}
		}
		return map;
	}
	
	
}
